package com.bbx.taxi.client.Activity.Order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Return.Official_VclassInfo;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.OfficialOrderList;
import com.bbx.api.sdk.model.official.passanger.Return.OrderList.UnprocessedList.UnprocessedList;
import com.bbx.taxi.client.Activity.MainActivity;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.ExamineMsg;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.Bean.Extra.OrderMsg;
import com.bbx.taxi.client.Task.MyCancelOrderTask;
import com.bbx.taxi.client.Task.MyCancelOrderTask.OnCancelOrderListener;
import com.bbx.taxi.client.Task.MyOperateTask;
import com.bbx.taxi.client.Task.MyOperateTask.OnOperateStatusListener;
import com.bbx.taxi.client.Task.MyOrderDetailTask;
import com.bbx.taxi.client.Task.MyOrderDetailTask.OrderDetailListener;
import com.bbx.taxi.client.Task.MyVclassInfoTask;
import com.bbx.taxi.client.Task.MyVclassInfoTask.OnVclassInfoListener;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.Tel;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;
import com.bbx.taxi.client.widget.Dailog.MyRejectDialog;
import com.bbx.taxi.client.widget.Dailog.MyRejectDialog.onRejectClickLeftListener;
import com.bbx.taxi.official.R;

/**
 * @Path com.bbx.taxi.client.Activity.Order.OrderExamine.java
 * @Author 严旭文
 * @Date 2016-1-19 下午4:14:12
 * @Company 蓝海（福建）信息技术有限公司
 * @Description 订单审核
 */
public class OrderUnprocessed extends BaseBBXActivity implements OnOperateStatusListener,OnVclassInfoListener,OrderDetailListener,OnCancelOrderListener{
	public static int code=11111;
	@InjectView(R.id.layout_examine)
	RelativeLayout layout_examine;
	@InjectView(R.id.tv_time)
	TextView tv_time;
	@InjectView(R.id.tv_name)
	TextView tv_name;
	@InjectView(R.id.tv_tel)
	TextView tv_tel;
	@InjectView(R.id.tv_appoint_time)
	TextView tv_appoint_time;
	@InjectView(R.id.tv_type)
	TextView tv_type;
	@InjectView(R.id.tv_start)
	TextView tv_start;
	@InjectView(R.id.tv_end)
	TextView tv_end;
	@InjectView(R.id.tv_car_models)
	TextView tv_car_models;
	@InjectView(R.id.tv_purpose)
	TextView tv_purpose;
	@InjectView(R.id.tv_estimate_price)
	TextView tv_estimate_price;
	@InjectView(R.id.btn_pass)
	Button btn_pass;
	@InjectView(R.id.btn_reject)
	Button btn_reject;
	@InjectView(R.id.layout_btn)
	RelativeLayout layout_btn;
	@InjectView(R.id.layout_pass)
	LinearLayout layout_pass;
	@InjectView(R.id.btn_again)
	Button btn_again;
	@InjectView(R.id.tv_wf)
	TextView tv_wf;
	@InjectView(R.id.layout_estimate_price)
	RelativeLayout layout_estimate_price;
	@InjectView(R.id.tv_taxi_price)
	TextView tv_taxi_price;
	@InjectView(R.id.tv_remark)
	TextView tv_remark;
	@InjectView(R.id.layout_tel)
	RelativeLayout layout_tel;
	public static OfficialOrderList mOfficialOrderList;
	public static UnprocessedList mUnprocessedList;
	public String type;
	public int status;
	public static String order_id;
	private Official_VclassInfo mOfficial_VclassInfo;
	private boolean isFinishResult=true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.order_examine);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initView() {
		setTitle(R.string.examine_order);
		layout_examine.setVisibility(View.GONE);
		btn_pass.setOnClickListener(this);
		btn_reject.setOnClickListener(this);
		btn_again.setOnClickListener(this);

	}

	public void setView(){
    	if(type.equals(ExamineMsg.TYPE_OF)){
    	if (mOfficialOrderList != null ) {
    		order_id=mOfficialOrderList.getOrder_id();
    		tv_time.setText(mOfficialOrderList.getOrder_time());
    		tv_name.setText(mOfficialOrderList.getOrderDetail().getStart().getName());
    		tv_tel.setText(mOfficialOrderList.getOrderDetail().getStart().getPhone());
    		tv_appoint_time.setText(mOfficialOrderList.getAppoint_time());
    		tv_start.setText(mOfficialOrderList.getOrderDetail().getStart().getAddress());
    		tv_end.setText(mOfficialOrderList.getOrderDetail().getEnd().getAddress());
    		if(mOfficialOrderList.getVehicleDetail()!=null){
    	 		tv_purpose.setText(mOfficialOrderList.getVehicleDetail().getPurpose());
    		}else{
    			tv_purpose.setText(mOfficialOrderList.getPurpose());
    		}
   
    		if(mOfficialOrderList.getPrice()!=null&&!mOfficialOrderList.getPrice().equals("")){
    			layout_estimate_price.setVisibility(View.VISIBLE);
    			tv_estimate_price.setText(FormatUtil.onFormatPrice(Integer.parseInt(mOfficialOrderList.getPrice()))); 
			}
     		//审核状态
    		String audit_status=mOfficialOrderList.getAudit_status();
    		if(audit_status!=null&&audit_status.equals(Value.STATUS_AUDIT_NOPASS)&&mOfficialOrderList.getVehicleDetail()!=null){
    			if(mOfficialOrderList.getVehicleDetail()!=null&&mOfficialOrderList.getVehicleDetail().getReject_reason()!=null){
    	   		   tv_remark.setText(String.format(getString(R.string.reject_reason2), mOfficialOrderList.getVehicleDetail().getReject_reason()));
    			   tv_remark.setVisibility(View.VISIBLE);
    			}else{
    				MyOrderDetailTask.getInstance(context, order_id).setOrderDetailListener(this);
    			}
        		layout_estimate_price.setVisibility(View.GONE);
        		if(status!=ExamineMsg.STATUS_PASS)setTitle(R.string.has_reject);
    		}else if(audit_status!=null&&audit_status.equals(Value.STATUS_AUDIT_PASS)){
    			tv_remark.setVisibility(View.GONE);
        		layout_estimate_price.setVisibility(View.VISIBLE);
        		if(status!=ExamineMsg.STATUS_PASS)setTitle(R.string.has_pass);
    		}else {//未审核
    			tv_remark.setVisibility(View.GONE);
    			layout_estimate_price.setVisibility(View.VISIBLE);
    			if(status!=ExamineMsg.STATUS_PASS){
        			setTitle(true, getString(R.string.examine_order), -1,
        					getString(R.string.mydailog_cancel));
        		}
    		}
    		if(mOfficialOrderList.getOrderInfo()!=null&&mOfficialOrderList.getOrderInfo().getVehicle_title()!=null&&!mOfficialOrderList.getOrderInfo().getVehicle_title().equals("")){
    			tv_car_models.setText(mOfficialOrderList.getOrderInfo().getVehicle_title());
    			if(mOfficialOrderList.getOrderInfo().getVtype()==Value.taxi_vtype){
    				setTaxi();
    			}
    		}else{
    			MyVclassInfoTask.getInstance(context, mOfficialOrderList.getTcId(), mOfficialOrderList.getVehicle_id()).setOnVclassInfoListener(this);
    		}
    		try{
    			tv_wf.setText(mOfficialOrderList.getRound_trip().equals(Value.ROUND_TRIP)?getString(R.string.yes):getString(R.string.no));
    		}
    		catch(Exception e){//额外添加的，用于主界面下单用的，通常订单列表没有OrderInfo字段
    			try{
    				tv_wf.setText(mOfficialOrderList.getOrderInfo().getRound_trip().equals(Value.ROUND_TRIP)?getString(R.string.yes):getString(R.string.no));
    			}
    			catch(Exception e1){
        			MyOrderDetailTask.getInstance(context, order_id).setOrderDetailListener(this);
    			}	
    		}
    		
        	layout_examine.setVisibility(View.VISIBLE);
		}else{
			MyOrderDetailTask.getInstance(context, order_id).setOrderDetailListener(this);
		}
    	}else if(type.equals(ExamineMsg.TYPE_UN)){
    		if (mUnprocessedList != null ) {
    			order_id=mUnprocessedList.getOrder_id();
        		tv_time.setText(mUnprocessedList.getOrderInfo().getAppoint_time());
        		tv_name.setText(mUnprocessedList.getOrderDetail().getStart().getName());
        		tv_tel.setText(mUnprocessedList.getOrderDetail().getStart().getPhone());
        		tv_appoint_time.setText(mUnprocessedList.getOrderInfo().getAppoint_time());
        		tv_start.setText(mUnprocessedList.getOrderDetail().getStart().getAddress());
        		tv_end.setText(mUnprocessedList.getOrderDetail().getEnd().getAddress());
        		tv_purpose.setText(mUnprocessedList.getPurpose());
        		if(mUnprocessedList.getOrderInfo().getPrice()!=-1){
        			layout_estimate_price.setVisibility(View.VISIBLE);
        			tv_estimate_price.setText(FormatUtil.onFormatPrice(mUnprocessedList.getOrderInfo().getPrice()));
    			}else{
    				layout_estimate_price.setVisibility(View.VISIBLE);
    				tv_estimate_price.setText(FormatUtil
    						.onFormatPrice(mUnprocessedList.getOrderInfo().getPrice()));
    			}
          		if(mUnprocessedList.getOrderInfo().getVehicle_title()!=null&&!mUnprocessedList.getOrderInfo().getVehicle_title().equals("")){
        			tv_car_models.setText(mUnprocessedList.getOrderInfo().getVehicle_title());
        		}else{
        			MyVclassInfoTask.getInstance(context,MainAttribute.getInstance().getTc_id(), mUnprocessedList.getOrderInfo().getVehicle_type_id()).setOnVclassInfoListener(this);
        		}
        		//驳回状态
        		String audit_status=mUnprocessedList.getProcessResult();
        		if(audit_status!=null&&audit_status!=null&&audit_status.equals(Value.STATUS_AUDIT_NOPASS)){
        	   		tv_remark.setText(String.format(getString(R.string.reject_reason2), mUnprocessedList.getRemark()));
        			tv_remark.setVisibility(View.VISIBLE);
            		layout_estimate_price.setVisibility(View.GONE);
            		if(status!=ExamineMsg.STATUS_PASS)setTitle(R.string.has_reject);
        		}else if(audit_status!=null&&audit_status.equals(Value.STATUS_AUDIT_PASS)){
        			tv_remark.setVisibility(View.GONE);
            		layout_estimate_price.setVisibility(View.VISIBLE);
            		if(status!=ExamineMsg.STATUS_PASS)setTitle(R.string.has_pass);
        		}else {//未审核
        			tv_remark.setVisibility(View.GONE);
        			layout_estimate_price.setVisibility(View.VISIBLE);
        			if(status!=ExamineMsg.STATUS_PASS){
        			setTitle(true, getString(R.string.examine_order), -1,
        					getString(R.string.mydailog_cancel));
        			}
        		}
     
        		tv_wf.setText(mUnprocessedList.getOrderInfo().getRound_trip().equals(Value.ROUND_TRIP)?getString(R.string.yes):getString(R.string.no));
        		layout_examine.setVisibility(View.VISIBLE);
    		}
    	}
		
    }

	@Override
	protected void initData() {
		Intent intent = getIntent();
		type = intent.getExtras().getString(ExamineMsg.EXAMINE_TYPE);
		status= intent.getExtras().getInt(ExamineMsg.EXAMINE_STATUS);
		if (type != null && !type.equals("")) {
			setView();
			setStatus(status);
		} else {
			startActivity(new Intent(context, OrderDelete.class));
		}
	}

	/**
	 * @param l
	 * @param orderid  改订单id最好不要是从OfficialOrderList中获取的
	 */
	public static void setOrder(OfficialOrderList l,String orderid) {
		mOfficialOrderList = l;
		order_id=orderid;
	}

	public static void setOrder(UnprocessedList l) {
		mUnprocessedList = l;
	}
    public void setStatus(int status){
    	switch(status){
    	case ExamineMsg.STATUS_PASS:
    		layout_btn.setVisibility(View.VISIBLE);	
    		layout_pass.setVisibility(View.VISIBLE);	
    		btn_again.setVisibility(View.GONE);
    		layout_tel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Tel.onTel(context, tv_tel.getText().toString().trim());
				}
			});
    		tv_tel.setTextColor(getResources().getColor(R.color.blue));
    		break;
    	case ExamineMsg.STATUS_AGAIN:
    		layout_btn.setVisibility(View.VISIBLE);	
    		layout_pass.setVisibility(View.GONE);	
    		btn_again.setVisibility(View.VISIBLE);
    		break;
    	case ExamineMsg.STATUS_NO:
    		layout_btn.setVisibility(View.GONE);	
    		break;
    	case ExamineMsg.STATUS_RECORD:
    		layout_btn.setVisibility(View.GONE);
            layout_tel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Tel.onTel(context, tv_tel.getText().toString().trim());
				}
			});
    		tv_tel.setTextColor(getResources().getColor(R.color.blue));
    		break;
    	}
    }
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_right:
			final MyAlertDailog dialog=new MyAlertDailog(this);
			dialog.setTitle(getString(R.string.mydailog_title));
			dialog.setContent(getString(R.string.mydailog_content_cancel));
			dialog.setLeftButtonText(getString(R.string.mydailog_giveup_cancel));
			dialog.setRightButtonText(getString(R.string.mydailog_confirm_cancel));
			if (dialog != null && !dialog.isShowing()) {
				dialog.show();
			}
			dialog.setOnClickLeftListener(new onClickLeftListener() {	
				@Override
				public void onClickLeft() {
					dialog.dismiss();
				}
			});
			dialog.setOnClickRightListener(new onClickRightListener() {
				
				@Override
				public void onClickRight() {
					dialog.dismiss();
					if(mOfficialOrderList!=null){
						MyCancelOrderTask.getInstance(context, order_id, mOfficialOrderList.getPrice(), 0).setOnCancelOrderListener(OrderUnprocessed.this);
					}else{
						ToastUtil.showToast(context, "订单不存在");
					}
					
				}
			});
			break;
		case R.id.btn_pass:
			MyOperateTask.getInstance(context, mApplication, order_id,Value.PASS, "")
			.setOnOperateStatusListener(this);
			break;
		case R.id.btn_reject:
			final MyRejectDialog rejectDialog = new MyRejectDialog(context);
			rejectDialog.setTitle(context.getResources().getString(
					R.string.reject_order));
			rejectDialog.setEditText(context.getResources().getString(
					R.string.reject_reason));
			rejectDialog.setLeftButtonText(context.getResources().getString(
					R.string.mydailog_confirm));
			rejectDialog.setRightButtonText(context.getResources().getString(
					R.string.mydailog_cancel));
			rejectDialog
					.setOnRejectClickLeftListener(new onRejectClickLeftListener() {
						@Override
						public void onRejectClickLeft() {
							if(rejectDialog.getEditText().equals("")){
								ToastUtil.showToast(context, R.string.reject_no_empty);
							}else{
								MyOperateTask.getInstance(context, mApplication, order_id,Value.NOPASS, rejectDialog.getEditText())
								.setOnOperateStatusListener(OrderUnprocessed.this);
								rejectDialog.dismiss();
							}
						
						}
					});
			rejectDialog.show();
			break;
		case R.id.btn_again:
			mOfficialOrderList.mOfficial_VclassInfo=mOfficial_VclassInfo;
			MainAttribute main = MainAttribute.getInstance();
			main.setAttribute(context,mOfficialOrderList);

			Intent intent = new Intent();
			intent.setClass(this, MainActivity.class);
			intent.putExtra(MainMsg.extra_agin_xiadan, MainMsg.AGAIB_XIADAN);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 注意本行的FLAG设置
			startActivity(intent);
			break;
		}
		super.onClick(v);
	}
	@Override
	protected void onDestroy() {
		mOfficialOrderList=null;
		mUnprocessedList=null;
		super.onDestroy();
	}
    public void setTaxi(){
    	 String audit_status=Value.STATUS_AUDIT_NO;
		if(mOfficialOrderList!=null){
			audit_status=mOfficialOrderList.getAudit_status();
		}
	
     //审核未通过不显示出租车
     if(audit_status!=null&&audit_status.equals(Value.STATUS_AUDIT_NOPASS)){
 		tv_taxi_price.setVisibility(View.GONE);
 		layout_estimate_price.setVisibility(View.GONE);
     }else{
 		tv_taxi_price.setVisibility(View.VISIBLE);
 		layout_estimate_price.setVisibility(View.INVISIBLE);
     }

    }
	@Override
	public void onOperateStatus(int status) {
		if(status==MyOperateTask.STATUS_SUCCESS){
			Bundle bundle = new Bundle();
			Intent intent = new Intent();
			intent.putExtras(bundle);
			setResult(resultCode, intent);
			onfinish();	
		}
	}

	@Override
	public void onVclassInfo(Official_VclassInfo mOfficial_VclassInfo,String class_id) {
		if(mOfficial_VclassInfo!=null&&mOfficial_VclassInfo.getInfo()!=null&&mOfficial_VclassInfo.getInfo().getVtype()!=null){
			this.mOfficial_VclassInfo=mOfficial_VclassInfo;
			int vtype = Integer.parseInt(mOfficial_VclassInfo.getInfo().getVtype());
			int vtype_index = Integer.parseInt(mOfficial_VclassInfo.getInfo().getLevel());
			switch(vtype){
			case 1:
			case 2:
				String type="";
				switch(vtype_index){
				case 1:
					type=Value.car_type[0];
					break;
				case 2:
					type=Value.car_type[1];
					break;
				case 3:
					type=Value.car_type[2];
					break;
				}
				tv_car_models.setText(mOfficial_VclassInfo.getInfo().getVname()+" "+type);
				break;
		
			case 3:
			case 4:
				tv_car_models.setText(mOfficial_VclassInfo.getInfo().getVname()+" "+mOfficial_VclassInfo.getInfo().getSeat_Num()+"座");
				break;
			case 5:
				tv_car_models.setText(mOfficial_VclassInfo.getInfo().getVname()+" "+mOfficial_VclassInfo.getInfo().getSeat_Num()+"座");
				setTaxi();
				break;
			}
	  }
	}
	@Override
	public void finish() {
		if(isFinishResult){
	    	Intent intent = new Intent();
		    setResult(resultCode, intent);
		}
		super.finish();
	}

	@Override
	public void onOrderDetail(int status, OfficialOrderList mOfficialOrderList) {
		this.mOfficialOrderList=mOfficialOrderList;
		setView();
	}

	@Override
	public void onCancelOrder(int status) {
		if(status==MyCancelOrderTask.STATUS_SUCCESS){
			Intent intent = new Intent(context,
					OrderCancelReason.class);
			intent.putExtra(OrderMsg.extra_order_id,order_id);
			startActivityForResult(intent, requestCode);
			isFinishResult=false;
			finish();
		}else{
			ToastUtil.showToast(context, R.string.no_network);
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==code){
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
